package com.xmy.jmm;

import java.util.concurrent.atomic.AtomicInteger;

public class AtomicDemo {

    private volatile static AtomicInteger num = new AtomicInteger();

    public static void add(){
        num.getAndIncrement(); //AtomicInteger+1 方法，使用CAS
    }

    public static void main(String[] args) {
        for (int i = 0; i < 20; i++) {
            new Thread(()->{
                for (int i1 = 0; i1 < 1000; i1++) {
                    add();
                }
            }).start();
        }
        while (Thread.activeCount()>2){

        }

        System.out.println(Thread.currentThread().getName()+" "+num);
    }
}
